<?php
/**
 * Product: rdc_analysis.
 * Date: 18-7-25
 * Time: 下午4:01
 */

namespace server;

use enums\HttpServerEnums;
use enums\ServiceEnums;
use libraries\CommUtils;

trait Request
{
    public function onRequest( $request, $response )
    {

        register_shutdown_function(array($this, 'handleFatal'));

        //--------------------------------------
        //version >= 1.8.6

        if (version_compare(SWOOLE_VERSION, '1.8.6') >= 0) {
            $this->fillGlobalVal($request, $response);
        }

        HttpServer::$response = $response;

        $sid = $this->application->session;

        if (isset($request->cookie[HttpServerEnums::SESSION_KEY]))
        {
            $_COOKIE = $request->cookie;


            $sid->setId($request->cookie[HttpServerEnums::SESSION_KEY]);
        }
        else
        {
            // 				$strSessionID = '' . sprintf( '%04X%04X%04X%04X%04X%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535),
            // 						mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151),
            // 						mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535) );

            $strSessionID = CommUtils::getUUIDNoDash();

            $sid->setId($strSessionID);


            $cookies = $this->application->di->get(ServiceEnums::SERVICE_COOKIES);
            $cookies->set(HttpServerEnums::SESSION_KEY, $strSessionID, 0, '/');

            $_SERVER['HTTP_COOKIE'] = HttpServerEnums::SESSION_KEY . '=' . $strSessionID . ';';
            $_COOKIE[HttpServerEnums::SESSION_KEY] = $strSessionID;

        }

        if (empty($request->server['path_info']))
        {
            $response->end('error request no path_info!<br>');

            return;
        }
        //
        //
        //if( false !== strpos( $request->server['path_info'], '/worker/status' ))
        //{
        //    $response->gzip(1);
        //    $response->end( print_r( $this->getWorkerStasusInfo(), true ) );
        //
        //    return;
        //}
        //
        //
        //if( false !== strpos( $request->server['path_info'], '/worker/dispatch' ) &&
        //    $this->_taskMgr->checkRequestValid( $request->server ) )
        //{
        //    $arrResInfo = [];
        //    $arrTasks = $this->_taskMgr->getTasks($request->server);
        //
        //    foreach ($arrTasks as $taskLabel)
        //    {
        //        $arrRet = $this->_taskMgr->dispatchTask( $taskLabel, $request, $this->_http );
        //
        //        foreach( $arrRet as $ret )
        //        {
        //            $arrResInfo[] = $ret;
        //        }
        //    }
        //
        //    $response->end(json_encode($arrResInfo) . PHP_EOL);
        //    return;
        //}
        //

        $strHtml = $this->application->handle($request->server['path_info'])->getContent();

        $response->gzip(1);
        $response->end($strHtml);
    }


    public function getWorkerStatus()
    {

    }


    private function fillGlobalVal( $request, $response )
    {
        if( isset($request->server) || isset( $request->header ))
        {//set server
            // 			$_SERVER = null;
            unset( $_SERVER );
            $_SERVER = array();

            $_SERVER = array_merge( $_SERVER, array_change_key_case( $request->server, CASE_UPPER ));
            $_SERVER = array_merge( $_SERVER, array_change_key_case( $request->header, CASE_UPPER ));

            //   no     $_SERVER[ 'REDIRECT_STATUS' ] = isset( $_SERVER[ 'status' ] ) ? $_SERVER[ 'status' ] : null;
            $_SERVER[ 'HTTP_HOST' ] = isset( $_SERVER[ 'HOST' ] ) ? $_SERVER[ 'HOST' ] : null;
            $_SERVER[ 'HTTP_USER_AGENT' ] = isset( $_SERVER[ 'USER-AGENT' ] ) ? $_SERVER[ 'USER-AGENT' ] : null;
            $_SERVER[ 'HTTP_ACCEPT' ] = isset( $_SERVER[ 'ACCEPT' ] ) ? $_SERVER[ 'ACCEPT' ] : null;
            $_SERVER[ 'HTTP_ACCEPT_LANGUAGE' ] = isset( $_SERVER[ 'ACCEPT-LANGUAGE' ] ) ? $_SERVER[ 'ACCEPT-LANGUAGE' ] : null;
            $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] = isset( $_SERVER[ 'ACCEPT_ENCODING' ] ) ? $_SERVER[ 'ACCEPT-ENCODING' ] : null;
            $_SERVER[ 'HTTP_REFERER' ] = isset( $_SERVER[ 'REFERER' ] ) ? $_SERVER[ 'REFERER' ] : null;

            if( isset( $request->cookie ))
            {
                $strCookie = '';
                foreach( $request->cookie as $k => $v)
                {
                    $strCookie = $k . '=' . $v . ';';
                }
            }

            $_SERVER[ 'HTTP_COOKIE' ] = isset( $strCookie ) ? $strCookie : null;

            $_SERVER[ 'HTTP_CONNECTION' ] = isset( $_SERVER[ 'CONNECTION' ] ) ? $_SERVER[ 'CONNECTION' ] : null;

            $_SERVER[ 'HTTP_UPGRADE_INSECURE_REQUESTS' ] = isset( $_SERVER[ 'UPGRADE-INSECURE-REQUESTS' ] ) ? $_SERVER[ 'UPGRADE-INSECURE-REQUESTS' ] : null;
            $_SERVER[ 'HTTP_CACHE_CONTROL' ] = isset( $_SERVER[ 'CACHE-CONTROL' ] ) ? $_SERVER[ 'CACHE-CONTROL' ] : null;
            $_SERVER[ 'CONTENT_TYPE' ] = isset( $_SERVER[ 'CONTENT-TYPE' ] ) ? $_SERVER[ 'CONTENT-TYPE' ] : null;
            $_SERVER[ 'CONTENT_LENGTH' ] = isset( $_SERVER[ 'CONTENT-LENGTH' ] ) ? $_SERVER[ 'CONTENT-LENGTH' ] : null;

            $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] = isset( $_SERVER[ 'X-REQUESTED-WITH' ] ) ? $_SERVER[ 'X-REQUESTED-WITH' ] : null;

        }
        else
        {
            $_SERVER = [];
        }

        //set $_GET
        if( isset($request->get) )
        {
            $_GET = $request->get;
            $_GET[ '_uri' ] = $request->server[ 'request_uri' ];
        }

        //set post
        if( isset( $request->post ) )
        {
            $_POST = $request->post;
        }

        //SET REQUEST
        $_REQUEST = array_merge( $_GET, $_POST );
    }
}